package com.github.bridge.client.esign;

import com.github.bridge.util.Const;
import com.github.bridge.util.JacksonUtil;
import com.timevale.esign.paas.tech.bean.request.OrganizeParam;
import com.timevale.esign.paas.tech.bean.request.PersonParam;
import com.timevale.esign.paas.tech.bean.result.AccountInfoResult;
import com.timevale.esign.paas.tech.bean.result.AddAccountResult;
import com.timevale.esign.paas.tech.client.ServiceClient;
import com.timevale.esign.paas.tech.client.ServiceClientManager;
import com.timevale.esign.paas.tech.enums.*;
import com.timevale.esign.paas.tech.service.AccountService;
import com.timevale.esign.paas.tech.service.TemplateSealService;
import com.timevale.esign.paas.tech.service.UserSignService;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@UtilityClass
public class ESignSdkClient {

    private final ServiceClient serviceClient;
    static {
        serviceClient = ServiceClientManager.get(Const.E_SIGN_APP_ID);
    }

    public void createOuterOrg(OrganizeParam organizeParam) {
        AccountService accountService = serviceClient.accountService();
        organizeParam.setOrgCode("org001");
        organizeParam.setName("机构001");
        organizeParam.setRegType(OrganRegTypeEnum.OTHER);
        AddAccountResult addAccountResult = accountService.addAccount(organizeParam);
        boolean success = addAccountResult.success();
        String accountId = addAccountResult.getAccountId();
        log.info("addAccountResult:{}", JacksonUtil.toJsonStr(addAccountResult));
    }

    public void createOuterUser(PersonParam personParam) {
        AccountService accountService = serviceClient.accountService();
        personParam.setName("zs");
        personParam.setIdNo("");
        personParam.setIdNoType(IdNoTypeEnum.MAINLAND);
        AddAccountResult addAccountResult = accountService.addAccount(personParam);
    }

    /**
     *
     * @param accountId 签署账号ID，通过创建个人/企业签署账号接口获取
     * @param encrypt 用户信息是否加密传输，默认 false
     */
    public void getAccountInfoByAccountId(String accountId, Boolean encrypt) {
        AccountService accountService = serviceClient.accountService();
        AccountInfoResult accountInfo = accountService.getAccountInfo(accountId, encrypt);
        log.info("accountInfo:{}", JacksonUtil.toJsonStr(accountInfo));
    }

    public void createOuterOrgSeal() {
        String accountId = "";
        // STAR-标准公章 OVAL-椭圆形印章 DEDICATED-圆形无五角星章
        OrganizeTemplateType star = OrganizeTemplateType.STAR;
        // 印章里的企业名称（生成印章中的上弦文） 与上面二选一
        String roundText = "";
        // 横向文件最多两行 每行20字
        List<String> hTexts = new ArrayList<>();
        // 生成印章中的下弦文内容（最多20字）；下弦文是指的贵司公章底部一串防伪数字，若没有该参数可不填。
        String qText = "";
        SealColor red = SealColor.RED;
        TemplateSealService templateSealService = serviceClient.templateSealService();
        templateSealService.createOrgSeal(accountId, star, roundText, hTexts, "", red);
    }

    public void createOuterUserSeal() {
        // String accountId,String name,PersonTemplateType type, SealColor color, StampRuleEnum stampRule
        String accountId = "";
        // 与上面二选一
        String name = "";
        // SQUARE-正方形印章 RECTANGLE-矩形印章 BORDERLESS-无框矩形印章
        PersonTemplateType personTemplateType = PersonTemplateType.BORDERLESS;
        SealColor red = SealColor.RED;
        // 印章是否加“印”规则 SEAL_NONE，无‘印’ SEAL_ONE，带‘印’ SEAL_TWO，带‘之印’
        StampRuleEnum rule = StampRuleEnum.SEAL_ONE;
        TemplateSealService templateSealService = serviceClient.templateSealService();
        templateSealService.createPsnSeal(accountId, name, personTemplateType, red, rule);
    }

    public void sign() {
        UserSignService userSignService = serviceClient.userSignService();
    }

}
